package com.budwk.app.sqfwV2.component.povertyMonitoring;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.exception.ServiceException;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.ExcelGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.ExcelRowDataParam;
import com.budwk.app.sqfwV2.dto.povertyMonitoring.FamilyMembersWorkDTO;
import com.budwk.app.sqfwV2.dto.povertyMonitoring.PersonWorkDTO;
import com.budwk.app.sqfwV2.dto.povertyMonitoring.PovertyAlleviationDTO;
import com.budwk.app.sqfwV2.enums.povertyMonitoring.FormingMaterialEnum;
import com.budwk.app.sqfwV2.enums.povertyMonitoring.PovertyMonitoringLabelEnum;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhangp
 * @description TODO
 * @className FamilyMembersComponent
 * @Date 15:50 2024/6/17
 */
@Slf4j
public class FamilyMembersComponent {

    public static void rowDataBuild(ExcelGenerateParam excelGenerateParam, String index) {
        List<ExcelRowDataParam> excelRowDataParamList = excelGenerateParam.getDataList();
        List<ExcelRowDataParam> excelRowDataParamListUpdate = Lists.newArrayList();
        excelGenerateParam.setCustomerParamMap(excelRowDataParamList.get(excelRowDataParamList.size() - 1).getCustomerParamMap());
        for (ExcelRowDataParam excelRowDataParam : excelRowDataParamList) {
            if (ObjectUtils.isEmpty(excelRowDataParam.getAbstractCustomerPdfCreateField())) {
                continue;
            }
            PovertyAlleviationDTO povertyAlleviationDTO = (PovertyAlleviationDTO) excelRowDataParam.getAbstractCustomerPdfCreateField();
            List<PersonWorkDTO> personWorkDTO = povertyAlleviationDTO.getPersonWorkInfoList();
            if (ObjectUtils.isNotEmpty(personWorkDTO) && !personWorkDTO.isEmpty()) {
                for (PersonWorkDTO item : personWorkDTO) {
                    //县内
                    if (String.valueOf(FormingMaterialEnum.A.getCode()).equals(index)) {
                        //非等于县内不加入
                        if (!PovertyMonitoringLabelEnum.livingSituation_one.getCode().equals(item.getLivingSituation())) {
                            continue;
                        }
                    }
                    //县外
                    if (String.valueOf(FormingMaterialEnum.B.getCode()).equals(index)) {
                        //非等于县外不加入
                        if (!PovertyMonitoringLabelEnum.livingSituation_two.getCode().equals(item.getLivingSituation())) {
                            continue;
                        }
                    }
                    FamilyMembersWorkDTO familyMembersWorkDTO = new FamilyMembersWorkDTO();
                    try {
                        BeanUtils.copyProperties(familyMembersWorkDTO, povertyAlleviationDTO);
                        //familyMembersWorkDTO.setHouseHoldNumber(povertyAlleviationDTO.getHouseHoldNumber());
                        if (PovertyMonitoringLabelEnum.monthLivCostItem_one.getCode().equals(item.getMonthLivCostItem())) {
                            familyMembersWorkDTO.setMonthLivCostItem_1(item.getMonthLivCost());
                        }
                        if (PovertyMonitoringLabelEnum.monthLivCostItem_two.getCode().equals(item.getMonthLivCostItem())) {
                            familyMembersWorkDTO.setMonthLivCostItem_2(item.getMonthLivCost());
                        }
                        if (PovertyMonitoringLabelEnum.monthLivCostItem_three.getCode().equals(item.getMonthLivCostItem())) {
                            familyMembersWorkDTO.setMonthLivCostItem_3(item.getMonthLivCost());
                        }
                        if (PovertyMonitoringLabelEnum.monthLivCostItem_four.getCode().equals(item.getMonthLivCostItem())) {
                            familyMembersWorkDTO.setMonthLivCostItem_4(item.getMonthLivCost());
                        }
                        familyMembersWorkDTO.setAge(povertyAlleviationDTO.getAge());
                        familyMembersWorkDTO.setName(povertyAlleviationDTO.getName());
                        familyMembersWorkDTO.setEnterpriseName(item.getEnterpriseName());
                        familyMembersWorkDTO.setPositionSituation(item.getPositionSituation());
                        familyMembersWorkDTO.setLivingPlace(item.getLivingPlace());
                        familyMembersWorkDTO.setLivingNote(item.getLivingNote());
                        familyMembersWorkDTO.setLivingHours(item.getLivingHours());
                        familyMembersWorkDTO.setTransTimes(item.getTransTimes());
                        familyMembersWorkDTO.setTransCost(item.getTransCost());
                        familyMembersWorkDTO.setLivingCondition(item.getLivingCondition());
                        //公益性
                        if (PovertyMonitoringLabelEnum.positionSituation_two.getCode().equals(item.getPositionSituation())) {
                            familyMembersWorkDTO.setEnterpriseName_gy(item.getEnterpriseName());
                            familyMembersWorkDTO.setMonthSalary_gy(item.getMonthSalary());
                            familyMembersWorkDTO.setGrossSalary_gy(item.getGrossSalary());
                            familyMembersWorkDTO.setEnterpriseName(null);
                            familyMembersWorkDTO.setMonthSalary(null);
                            familyMembersWorkDTO.setGrossSalary(null);
                        } else {
                            familyMembersWorkDTO.setEnterpriseName_gy(null);
                            familyMembersWorkDTO.setMonthSalary_gy(null);
                            familyMembersWorkDTO.setGrossSalary_gy(null);
                            familyMembersWorkDTO.setEnterpriseName(item.getEnterpriseName());
                            familyMembersWorkDTO.setMonthSalary(item.getMonthSalary());
                            familyMembersWorkDTO.setGrossSalary(item.getGrossSalary());
                        }
                        String start = DateUtil.format(item.getLivingStart(), DatePattern.CHINESE_DATE_PATTERN);
                        String end = DateUtil.format(item.getLivingEnd(), DatePattern.CHINESE_DATE_PATTERN);
                        familyMembersWorkDTO.setLivingStartEnd(String.format("%s-%s", StringUtils.isBlank(start)||"null".equals(start) ? "" : start, StringUtils.isBlank(end)|| "null".equals(end) ? "" : start));
                        ExcelRowDataParam excelRowDataParamNew =new ExcelRowDataParam();
                        BeanUtils.copyProperties(excelRowDataParamNew,excelRowDataParam);
                        excelRowDataParamNew.setAbstractCustomerPdfCreateField(familyMembersWorkDTO);
                        excelRowDataParamListUpdate.add(excelRowDataParamNew);
                    } catch (Exception e) {
                        log.error("构建家庭成员务工表数据出错！");
                        throw new ServiceException("构建家庭成员务工表数据出错:" + e.getMessage());
                    }
                }
            }
        }
        if (!excelRowDataParamListUpdate.isEmpty()) {
            excelRowDataParamListUpdate.stream().sorted(Comparator.comparing(s -> JSONObject.parseObject(JSONObject.toJSONString(s.getAbstractCustomerPdfCreateField()), FamilyMembersWorkDTO.class).getHouseHoldNumber())).collect(Collectors.toList());
        }
        excelGenerateParam.setDataList(excelRowDataParamListUpdate);
    }

}
